package com.njupt.simulation.utils;

import uk.me.g4dpz.satellite.*;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;

public class TLEtoLLA {

    /**
     * @param sattle_data 三行星历
     * @param sat_time    计算的时间
     * @return
     */
    public static double[] get_sat_only_location_info(String[] sattle_data, long sat_time) {
        return get_sat_only_location_info(sattle_data, sat_time, new double[]{0, 0, 0});
    }

    /**
     *
     * @param sattle_data 三行星历
     * @param sat_time 计算的时间
     * @param groundstation_array 地球站位置，如果没有[0，0，0]
     * @return
     */
    public static double[] get_sat_only_location_info (String[] sattle_data, long sat_time, double[] groundstation_array) {
        try {
            double[] sat_locations = new double[3];
            Satellite satellite = null;

            TLE tle = new TLE(sattle_data);
            satellite = SatelliteFactory.createSatellite(tle);

            GroundStationPosition groundStationPosition = new GroundStationPosition(groundstation_array[1], groundstation_array[0], groundstation_array[2]);

            Date sat_date = new Date(sat_time);
            //System.out.println(sat_date);
            //第二个参数传入一个时间来确定卫星在该时刻的位置
            SatPos satPos = satellite.getPosition(groundStationPosition, sat_date);

            double longitude = satPos.getLongitude();
            double latitude = satPos.getLatitude();

            double longitude_temp = (longitude * 180/Math.PI);
            if(longitude_temp>180&&longitude_temp<360){
                double longitude_temp1 = longitude_temp - 360;
                sat_locations[0] = longitude_temp1;
            }else {
                sat_locations[0] = longitude_temp;
            }
            //sat_locations[0] = longitude_temp;
            double latitude_temp = latitude *  180/Math.PI;


            sat_locations[1] = latitude_temp;
            sat_locations[2] = satPos.getAltitude(); //卫星距地面的距离(千米)

            return sat_locations;
        }catch (Exception e){
            e.printStackTrace();
            throw e;
        }
    }

//    public static void main(String[] args) {
//
////        commandDAO commandDAO = new commandDAO();
////        command command = commandDAO.getupcommandagainstById(1);
//
////        long seconds = calculateSecondsBetween(command.getStarttime(), command.getEndtime());
////        int timeinterval= command.getTimeinterval();
//
//        // 定义日期格式
//        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
//        // 将日期字符串转换为 Date 对象
////        commandDAO commandDAO = new commandDAO();
////        command command = commandDAO.getdowncommandById(1);
////        String dateString = command.getStarttime();
//
//       // LocalDateTime dateTime = LocalDateTime.parse(dateString, formatter);
//        double timeinterval = 1;
//        for (int i = 0;i<86400;i+=timeinterval){
//
//            String[] sattle_data = {
//                    "STARLINK-1013",
//                    "1 44719U 19074G   24317.08280947  .00019537  00000+0  13256-2 0  9994",
//                    "2 44719  53.0540 193.1229 0000264 162.8986 197.2012 15.06434085276174"
//            };
////            "STARLINK-1008",
////                    "1 44714U 19074B   24316.69948730 -.00008762  00000+0 -57027-3 0  9991",
////                    "2 44714  53.0534 194.8485 0001019  80.3251 279.7853 15.06386258276124"
//
//
//
////            "STARLINK-1028",
////                    "1 44733U 19074W   24317.25001157  .01192349  00000+0  54563-1 0  9992",
////                    "2 44733  53.0517 192.1797 0001369  90.9567 294.9082 15.17072687276200"
//
//            double[] groundstation_array = new double[]{0, 0, 0};
//            dateTime = dateTime.plusSeconds((long) timeinterval);
//            // 将 LocalDateTime 转换为带有时区的 ZonedDateTime（北京时间，UTC+8）
//            ZonedDateTime zonedDateTime = dateTime.atZone(ZoneId.of("Asia/Shanghai"));
//            // 将 ZonedDateTime 转换为时间戳（毫秒）
//            long timestamp = zonedDateTime.toInstant().toEpochMilli();
//
//            double[] leosatellitecoordinate = get_sat_only_location_info(sattle_data,timestamp, groundstation_array);
//            if((leosatellitecoordinate[0] >= 111 && leosatellitecoordinate[0]<=120)&&leosatellitecoordinate[1]>=25 && leosatellitecoordinate[1]<=35){
//                for (int ii = 0;ii<3;ii++){
//                    System.out.print(leosatellitecoordinate[ii]+" ");
//                }
//                System.out.print("\n");
//            }
////            for (int ii = 0;ii<3;ii++){
////                System.out.print(leosatellitecoordinate[ii]+" ");
////            }
////            System.out.print("\n");
//        }
//    }
}